package com.cn.delan.core.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 验证码生成类
 * @author BingGo
 *
 */
public class ValidCodeUtil{
	
	/**
	 * 随机码生成字典
	 */
	private static char[] captchars = new char[] { 
			'1', '2', '3', '4', '5', '6', '7', '8', '9',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
			'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
			's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

	private static Font getFont(int fontSize){
		return new Font("Courier", Font.PLAIN, fontSize);
	}
	/**
	 * 随机产生定义的颜色
	 * @return
	 */
	private static Color getColor() {
		Color color[] = new Color[10];
		color[0] = new Color(32, 158, 25);
		color[1] = new Color(218, 42, 19);
		color[2] = new Color(31, 75, 208);
		return color[MathUtil.randInt(3)];
	}
	/**
	 * 产生随机码
	 * @param codeLength 随机码的长度
	 * @return
	 */
	private static String getRandCode(int codeLength){
		String code = "";
		for (int i = 0; i < codeLength; i++) {
			code += captchars[MathUtil.randInt(captchars.length - 1) + 1];
		}
		return code;
	}
	/**
	 * 将code以指定字号大小绘制在指定宽高的内存图像对象中
	 * @param width 图像宽度
	 * @param height 图像高度
	 * @param code 要绘制的字符串
	 * @param fontSize 字号大小
	 * @return 返回绘制好的内存中的图像对象
	 */
	private static BufferedImage paint(int width,int height,String code,int fontSize){

		Font font = ValidCodeUtil.getFont(fontSize);
		Color bgColor = Color.WHITE;

		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TYPE_BYTE_INDEXED);

		Graphics2D graphics = bi.createGraphics();
		//设置背景色
		graphics.setColor(bgColor);
		//填充背景
		graphics.fillRect(0, 0, bi.getWidth(), bi.getHeight());
		
		TextLayout textTl = new TextLayout(code, font, new FontRenderContext(null, true, false));
		graphics.setFont(font);
		graphics.setColor(ValidCodeUtil.getColor());
		graphics.drawString(code, 0, fontSize);
		//打印重影
		textTl.draw(graphics, 4, fontSize+4);
		//加入干扰线
		for(int i=0;i<fontSize/2;i++){
			int x1 = MathUtil.randInt(width) + 1;
			int y1 = MathUtil.randInt(height) + 1;
			int x2 = MathUtil.randInt(width) + 1;
			int y2 = MathUtil.randInt(height) + 1;
			graphics.setColor(ValidCodeUtil.getColor());
			graphics.drawLine(x1, y1, x2, y2);
		}
		//变形处理
//		ValidCodeUtil.shear(graphics, w, h, bgColor);
		ImageUtil.turnWave(bi, 5);
		ImageUtil.turnWave(bi, 3);
		return bi;
	}
	public static String createValidCodeImage(int fontSize,int codeLength,OutputStream out) throws IOException{
		//宽度校正8/13
		int width = fontSize*codeLength*8/13;
		//高度校正1.5倍
		int height = fontSize*3/2;
		//产生随机字符串
		String code = ValidCodeUtil.getRandCode(codeLength);
		BufferedImage bi = ValidCodeUtil.paint(width, height, code, fontSize);
		ImageUtil.write(bi,"jpeg", out);
		return code;
	}
	
	public static void main(String [] args) throws IOException{
		System.out.println(ValidCodeUtil.createValidCodeImage(60,4,new FileOutputStream("validcode.jpg")));
	}

}
